#!/bin/bash
#
# Copyright (C) 2013-2021 Spring Studio
# Copyright (C) 2013-2021 Dou Dongchun <doudongchun@163.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

########### library variables ##########
URI=
GIT=https://gitee.com/springstudio/PROJ.git
COMMIT=5.2.0
LOCAL=proj
PLATFORM=

########### build library ##########

build_arch()
{
  # fix cmake test error
  sed -i "s/add_subdirectory(test)//g" $BUILD_ROOT/proj/CMakeLists.txt
  # fix https://github.com/OSGeo/PROJ/issues/1235
  sed -i "s/..\/src\/nad2bin/nad2bin/g" $BUILD_ROOT/proj/nad/Makefile.am

  if [[ $PLATFORM == "cmake" ]]; then
    configure_cmake
  else
    configure_autoconf
  fi

  make clean
  make -j4 -s
  make install
}

configure_autoconf()
{
  ./autogen.sh

  rm -rf build
  mkdir build
  cd build

  export PKG_CONFIG_PATH=$PREFIX/lib/pkgconfig
  # export CPPFLAGS=-I$PREFIX/include
  # export LDFLAGS=-L$PREFIX/lib

  ../configure \
    --prefix=$PREFIX \
    --host=$TARGET \
    --with-sysroot=$SYSROOT
}

configure_cmake()
{
  rm -rf build
  mkdir build
  cd build

   cmake \
     -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
     -DCMAKE_INSTALL_PREFIX=$PREFIX \
     -DCMAKE_BUILD_TYPE=Release \
     -DANDROID_PLATFORM=$API \
     -DANDROID_ABI=$ABI \
     -DANDROID_NDK=$ANDROID_NDK \
     -DANDROID_TOOLCHAIN=clang \
     -DBUILD_SHARED_LIBS=ON \
     -DBUILD_GMOCK=OFF \
     -DBUILD_GTEST=OFF \
     -DPROJ_TESTS=OFF \
     -DBUILD_GIE=OFF \
     ..
}

clear_arch() {
  if [ -d build ]; then
    cd build && make clean && cd -
    rm -rf build
  fi
}

PS3='Enter your compile platform: '
options=("autoconf" "cmake")
select opt in "${options[@]}"
do
    case $opt in
        autoconf|cmake)
            PLATFORM=$opt
            break
            ;;
        *) echo "invalid option $REPLY";;
    esac
done

############# forbid modify ###########

. ./tools/do-compile.sh
